/***************************************************************************************************/
/* @date on 2020/08/28                                                                             */
/* @author Xie Jian                                                                                */
/* @email 190954950@qq.com                                                                         */
/* @describe 写日志类                                                                               */
/***************************************************************************************************/
use crate::jtool::dir;
use crate::jtool::file;
use crate::jtool::run_context;
use chrono::prelude::*;
use std::fs::OpenOptions;
use std::io::Write;

/// 写入调试日志
///
/// # Examples
///
/// ```
/// debug("日志");
/// ```
pub fn debug(content: &str) {
    let log_dir = get_log_dir("debug");
    write_log(&log_dir, content);
}

/// 写入信息日志
///
/// # Examples
///
/// ```
/// info("日志");
/// ```
pub fn info(content: &str) {
    let log_dir = get_log_dir("info");
    write_log(&log_dir, content);
}

/// 写入错误日志
///
/// # Examples
///
/// ```
/// error("日志");
/// ```
pub fn error(content: &str) {
    let log_dir = get_log_dir("error");
    write_log(&log_dir, content);
}

/// 写入成功日志
///
/// # Examples
///
/// ```
/// success("日志");
/// ```
pub fn success(content: &str) {
    let log_dir = get_log_dir("success");
    write_log(&log_dir, content);
}

/// 写入异常日志
///
/// # Examples
///
/// ```
/// exception("日志");
/// ```
pub fn exception(content: &str) {
    let log_dir = get_log_dir("exception");
    write_log(&log_dir, content);
}

/// 写入自定义日志
///
/// # Examples
///
/// ```
/// custom("自定义文件夹","日志");
/// ```
pub fn custom(folder: &str, content: &str) {
    let log_dir = get_log_dir(folder);
    write_log(&log_dir, content);
}

/// 获取日志的目录
///
/// # Examples
///
/// ```
/// get_log_dir("目录");
/// ```
fn get_log_dir(name: &str) -> String {
    let path = run_context::get_run_dir();
    let log_dir = path + "\\zaka_log";
    let is_exist_dir = dir::is_exist(&log_dir);
    if !is_exist_dir {
        dir::create(&log_dir);
    }

    let log_info_dir = log_dir + "\\" + name;
    let is_exist_dir = dir::is_exist(&log_info_dir);
    if !is_exist_dir {
        dir::create(&log_info_dir);
    }

    return log_info_dir;
}

/// 写日志
///
/// # Examples
///
/// ```
/// write_log("目录","日志信息");
/// ```
fn write_log(path: &str, content: &str) {
    let now: NaiveDateTime = Local::now().naive_local();
    let time_string = now.format("%Y-%m-%d").to_string();
    let file_path = path.to_string() + "\\" + &time_string + ".txt";
    let file_exist = file::is_exist(&file_path);
    if !file_exist {
        file::create_file(&file_path);
    }

    let now: NaiveDateTime = Local::now().naive_local();
    let time_string = now.format("%Y-%m-%d %H:%M:%S").to_string();
    let content_full = "『".to_string() + &time_string + "』" + content + "\n";

    let mut file = OpenOptions::new()
        .append(true)
        .open(file_path)
        .expect("cannot open file");
    file.write_all(content_full.as_bytes())
        .expect("write failed");
}
